<?php

session_start();

require_once("includes/connect.php");
require_once("includes/function.php");

$task = $_REQUEST["task"];
$pet_id = $_REQUEST["pet_id"];
$id = $_REQUEST["id"];

$reason = $_REQUEST["reason"];

$i_date = $_REQUEST["i_date"];

$cheque_no = $_REQUEST["cheque_no"];
$branch_name = $_REQUEST["branch_name"];
$bank_name = $_REQUEST["bank_name"];

$month = $_REQUEST["month"];
$year = $_REQUEST["year"];

$val = $_REQUEST["val"];

$val1 = explode(",", $val);
$val1 = implode("','", $val1);

$cancel_date = $_REQUEST["cancel_date"];
$cancel_reason = $_REQUEST["cancel_reason"];

$payment_method = $_REQUEST["payment_method"];

$db = new myDBS;
$db->selectDB("account");

switch ($task) {
    case "verify":
        $budget_year = (date("Y") < 2556) ? date("Y") + 543 : date("Y");

        $finance_id = get_running_finance($budget_year);

        $sql = "UPDATE tbl_salary "
                . "SET verify_status = 'ผ่าน'"
                . ", verify_date = '$i_date' "
                . ", finance_id = '$finance_id' "
//            . ", payment_method = '$payment_method'  "
                . " WHERE pet_id = '$pet_id'";

        break;
    case "cancel":
//        $sql = "UPDATE tbl_salary "
//            . " SET verify_status = 'ยกเลิก', reason = '$reason', verify_date = NOW() "
//            . " WHERE  pet_id = '$pet_id'";

        $sql = "UPDATE tbl_salary "
                . " SET status = 'ยกเลิก' "
                . ", cancel_reason = '$cancel_reason' "
                . ", cancel_date = '$cancel_date' "
                . "  WHERE  pet_id = '$id'";

        break;
    case "approved":
        $sql = "UPDATE tbl_salary SET approved_status = 'อนุมัติ', approved_date = '$i_date'  WHERE pet_id = '$pet_id'";

        break;
    case "disapproval":
        $sql = "UPDATE tbl_salary SET approved_status = 'ไม่อนุมัติ', approved_date = NOW() WHERE pet_id = '$pet_id'";

        break;
    case "approvedCheque":
        $sql = "UPDATE tbl_salary SET cheque_status = 'อนุมัติ', cheque_date = '$i_date' WHERE cheque_no = '$cheque_no'";

        break;
    case "disapprovalCheque":
        $sql = "UPDATE tbl_salary SET cheque_status = 'ไม่อนุมัติ' WHERE cheque_no = '$cheque_no'";

        break;
    case "print":
        $sql = "UPDATE tbl_salary SET cheque_no = '$cheque_no', branch_name = '$branch_name', bank_name = '$bank_name', print_cheque_date = '$i_date', print_cheque_status = 'พิมพ์แล้ว' WHERE id in('$val1') and lvl1_id = '" . $_SESSION["lvl1_id"] . "' and approved_status =  'อนุมัติ' and month_year = '$year-$month'";

        break;
    case "paid":
        $sql = "UPDATE tbl_salary SET paid_status = 'จ่ายแล้ว', paid_date = NOW() WHERE cheque_no = '$cheque_no'";
        break;
}
//echo $sql;
$db->runSQL($sql) or die(mysql_error());
?>
